﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;

#endregion 

namespace ScrumTable.Common.Data.Tracking
{

    /// <summary>
    /// Defines a method to release allocated resources.
    /// </summary>
    public class RevisionManager : ICloneable
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private double _baseRevision = 0.0;
        private double _currentRevision = 0.0;
        private double _initialRevision = 0.0;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the current revision as double.
        /// </summary>
        public double Current
        {
            get { return _currentRevision; }
        }

        /// <summary>
        /// Returns true, if the current revision is initial (before the first value has been set).
        /// </summary>
        /// <remarks>
        /// This means that current revision hasn't changed yet.
        /// </remarks>
        public bool IsInitial
        {
            get { return (_currentRevision == _initialRevision); }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new RevisionManager instance, and initializes the
        /// base revision.
        /// </summary>
        public RevisionManager()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        object ICloneable.Clone()
        {
            return Clone();
        }

        /// <summary>
        /// Resets the revision to its initial revision.
        /// </summary>
        public void Reset()
        {
            _currentRevision = _initialRevision;
        }

        /// <summary>
        /// Updates the revision counter to the next revision.
        /// </summary>
        public void Next()
        {
            _currentRevision = IsInitial ? ++_baseRevision : ++_currentRevision;
        }

        /// <summary>
        /// Clones the current revision and creates a new branch on the 
        /// revision tree.
        /// </summary>
        /// <returns>Returns the cloned revision instance.</returns>
        public RevisionManager Clone()
        {
            return Branch();
        }

        private RevisionManager Branch()
        {
            RevisionManager branch = new RevisionManager();
            branch._initialRevision = _currentRevision;
            branch._currentRevision = _currentRevision;
            branch._baseRevision = _currentRevision / Math.Pow(10, Convert.ToInt32(Current).ToString().Length);
            return branch;
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}